package com.ec.mn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ec.mn.entity.HealthRecord;
import com.ec.mn.entity.Notification;
import com.ec.mn.mapper.NotificationMapper;
import com.ec.mn.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements NotificationService {

    @Autowired
    private NotificationMapper notificationMapper;

    @Override
    public List<Notification> getByReceiverId(Long receiverId) {
        return notificationMapper.selectByReceiverId(receiverId);
    }

    @Override
    public List<Notification> getBySenderId(Long senderId) {
        return notificationMapper.selectBySenderId(senderId);
    }

    @Override
    public List<Notification> getUnreadByReceiverId(Long receiverId) {
        return notificationMapper.selectUnreadByReceiverId(receiverId);
    }

    @Override
    public List<Notification> getReadByReceiverId(Long receiverId) {
        return notificationMapper.selectReadByReceiverId(receiverId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean markAsRead(Long id) {
        return notificationMapper.markAsRead(id, LocalDateTime.now()) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean markAsDeleted(Long id) {
        return notificationMapper.markAsDeleted(id) > 0;
    }

    @Override
    public Long countUnreadByReceiverId(Long receiverId) {
        return notificationMapper.countUnreadByReceiverId(receiverId);
    }

    @Override
    public Long countReadByReceiverId(Long receiverId) {
        return notificationMapper.countReadByReceiverId(receiverId);
    }

    @Override
    public List<Notification> getByTimeRange(Long receiverId, LocalDateTime startTime, LocalDateTime endTime) {
        return notificationMapper.selectByTimeRange(receiverId, startTime, endTime);
    }

    @Override
    public IPage<Notification> getPageByReceiverId(Page<Notification> page, Long receiverId) {
        return notificationMapper.selectPageByReceiverId(page, receiverId);
    }

    @Override
    public boolean validateNotification(Notification notification) {
        if (notification == null) return false;
        if (!StringUtils.hasText(notification.getContent())) return false;
        if (notification.getReceiverId() == null) return false;
        return true;
    }
    @Override
    public void add(HealthRecord healthRecord,String content){
        if(healthRecord.getIsAbnormal()==1){
            Notification notice=new Notification();
            notice.setSenderId((healthRecord.getCreatedByAdminId()));
            notice.setSendTime(LocalDateTime.now());
            notice.setReceiverId(healthRecord.getElderlyUserId());
            notice.setContent(content);
            notice.setStatus(0);
            save(notice);
        }

    }
} 